<?php
namespace Admin\Controller;
use Think\Controller;
//猜歌巨星
class GuessSongController extends CommonController {

    public function index(){
        self::user_list();
    }

    //用户模块
    public function user_list()
    {
        //暂时不做分页
        $data['user_list']  =   M('song_member')->select();
        $this->assign("data",$data);
        $this->display('user_list');
    }

    //更多好玩
    public function game_more()
    {
        $data['game_list']  =   M('song_appshow')->select();
        $this->assign("data",$data);
        $this->display('game_more');
    }

    public function game_more_add()
    {
        if (IS_AJAX) 
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('song_appshow')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_more_add');
        }
    }

    public function game_more_edit()
    {
        if (IS_AJAX) 
        {
            $where['song_appshow_id']  =   I('post.song_appshow_id','','htmlspecialchars');
            if (empty($where['star_moreplay_id'])) 
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');    
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('song_appshow')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['song_appshow_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['song_appshow_id']))
            {
                $this->error('非法请求');
            }

            $data['game_info']  =   M('song_appshow')->where($where)->find();

            if ( empty($data)) 
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_more_edit');
        }
    }

    //游戏题库
    public function game_question()
    {
        $data['game_question']  =   M('song_quersion')->order('song_quersion_id desc')->select();
        $this->assign("data",$data);
        $this->display('game_question');
    }

    public function game_question_add()
    {
        if (IS_AJAX) 
        {
            $data['singer']     =   I('post.singer','','htmlspecialchars');
            $data['album']      =   I('post.album','','htmlspecialchars');
            $data['songname']   =   I('post.songname','','htmlspecialchars');
            $data['sort']       =   I('post.sort','','htmlspecialchars');
            $data['songb']      =   I('post.songb','','htmlspecialchars');
            $data['songa']      =   I('post.songa','','htmlspecialchars');
            $data['answer']     =   I('post.answer','','htmlspecialchars');
            $data['question']   =   I('post.question','','htmlspecialchars');
            $data['sort']       =   I('post.sort','','htmlspecialchars');
            $data['puzzle1']    =   I('post.puzzle1','','htmlspecialchars');
            $data['puzzle2']    =   I('post.puzzle2','','htmlspecialchars');
            $data['puzzle3']    =   I('post.puzzle3','','htmlspecialchars');
            $data['puzzle4']    =   I('post.puzzle4','','htmlspecialchars');

            $result     =   M('song_quersion')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_question_add');
        }
    }

    public function game_question_edit()
    {

        if (IS_AJAX) 
        {
            $where['song_quersion_id']  =   I('post.song_quersion_id','','htmlspecialchars');
            if (empty($where['song_quersion_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');    
            }

            $data['singer']     =   I('post.singer','','htmlspecialchars');
            $data['album']      =   I('post.album','','htmlspecialchars');
            $data['songname']   =   I('post.songname','','htmlspecialchars');
            $data['sort']       =   I('post.sort','','htmlspecialchars');
            $data['songb']      =   I('post.songb','','htmlspecialchars');
            $data['songa']      =   I('post.songa','','htmlspecialchars');
            $data['answer']     =   I('post.answer','','htmlspecialchars');
            $data['question']   =   I('post.question','','htmlspecialchars');
            $data['sort']       =   I('post.sort','','htmlspecialchars');
            $data['puzzle1']    =   I('post.puzzle1','','htmlspecialchars');
            $data['puzzle2']    =   I('post.puzzle2','','htmlspecialchars');
            $data['puzzle3']    =   I('post.puzzle3','','htmlspecialchars');
            $data['puzzle4']    =   I('post.puzzle4','','htmlspecialchars');

            $result     =   M('song_quersion')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['song_quersion_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['song_quersion_id']))
            {
                $this->error('非法请求');
            }

            $data['game_question']  =   M('song_quersion')->where($where)->find();

            if ( empty($data)) 
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_question_edit');
        }
    }

    //分享微信群
    public function wechat_group()
    {
        $data['wechat_group']  =   M('song_interior_group')->order('song_wechatgroup_id desc')->select();
        $this->assign("data",$data);
        $this->display('wechat_group');
    }

    //游戏签到
    public function game_signin()
    {
        $order_by['song_signin_id']   =   'desc';
        $field                        =   array('song_member_id','song_member_id','signin_num','signin_time');
        $data['song_signin']          =   M('song_signin')->order($order_by)->field($field)->select();
        $this->assign("data",$data);
        $this->display('game_signin');
    }

    //礼物领取记录
    public function game_getgift()
    {
        $order_by['song_getgift_id']  =   'desc';
        $field                        =   array('song_getgift_id','song_member_id','song_gift_id','createtime','getcode','issend');
        $data['song_getgift']         =   M('song_getgift')->order($order_by)->field($field)->select();

        $song_gift                    =   M('song_gift')->field('song_gift_id,giftname')->select();

        $gift_list  =   array();
        foreach ($song_gift as $val)
        {
            $gift_list[$val['song_gift_id']]    =   $val;
        }

        foreach ($data['song_getgift'] as $key => $val)
        {
            if (! empty($gift_list[$val['song_gift_id']]))
            {
                $data['song_getgift'][$key]['giftname'] =   $gift_list[$val['song_gift_id']]['giftname'];
            }
            else
            {
                $data['song_getgift'][$key]['giftname']     =   '';
            }
        }

        $this->assign("data",$data);
        $this->display('game_getgift');
    }


}